var TreatedTaskManaged = function() {
	return {
		datatable : null,
		init : function() {
			this.initSearch();
			this.initListener();
		},
		initSearch : function() {
			if (!jQuery().dataTable) {
				return;
			}
			var pictureTable = $("#taskListTable").dataTable({
				"sAjaxSource" : ctx + "/process/task/treated/search?now=" + new Date().getTime(),
				/* 使用post方式 */
				"fnServerData" : function(sSource, aoData, fnCallback) {
					var postData = {
						page : 1,
						start : 0,
						rows : 10,
						params : {}
					};
					// 参数
					for (var i = 0, size = aoData.length; i < size; i++) {
						if (aoData[i].name == 'iDisplayLength') {
							postData.rows = aoData[i].value;
						} else if (aoData[i].name == 'iDisplayStart') {
							postData.start = aoData[i].value;
						} else if (aoData[i].name == 'sEcho') {
							postData.page = aoData[i].value;
						}
					}

					if($('#name').val()){
						postData.params.applyer = $('#name').val();
					}
					
					$.ajax({
						"dataType" : 'json',
						"type" : "POST",
						"cache": false,
						"url" : sSource,
						"data" : postData,
						"success" : function(data, textStatus, jqXHR){
							fnCallback(data.data, textStatus, jqXHR);
						}
					});
				},
				"bAutoWidth":false,
				"aoColumnDefs" : [
						
						{
							"data" : "index",
							"aTargets" : [ 0 ]
						},{
							"data" : "processName",
							"aTargets" : [ 1 ]
						},{
							"data" : "applyer",
							"aTargets" : [ 2 ]
						},
						{
							"data" : "name",
							"aTargets" : [ 3 ]
						},
						{
							"data" : "createTime",
							"aTargets" : [ 4 ]
						},
						{
							"aTargets" : [ 5 ],
							"mRender" : function(data, type, full) {
								var btns =  '';
								btns += '<button class="btn btn-sm-table  btn-primary" name="viewDetail" target_id="'+full.billID+'" task_id="'+full.id+'" processDefinitionId="'+full.processDefinitionId+'">查看详情</button>';
								btns += '<button class="btn btn-sm-table  btn-primary" name="viewExeProcess" target_id="'+full.billID+'" task_id="'+full.id+'" >流程执行过程</button>';
								btns += '<button class="btn btn-sm-table  btn-primary" name="viewComment" target_id="'+full.billID+'" task_id="'+full.id+'" >历史批注</button>';
								//btns +=  Permission.createPermElement('process-deploy:list:del',' <button title="'+messages.public_button_del+'" class="btn btn-sm-table  btn-danger" name="del" target_id="'+full.id+'">删除</button>');
								return btns;
							}
						} ]
			});
			this.datatable = pictureTable;
		},
		reSearch:function(){
        	this.datatable.fnDraw();
        },removeClass:function(){//去除Form表单的验证样式
        	$('.form-group','#leaveAuditForm','#overtimeAuditForm').removeClass('has-success')
            .removeClass('has-error');
    		 $('span').remove('.help-block');
        },initListener : function() {
			/*
			 * 查询
			 */
			$("#searchBtn").click(function() {
				TreatedTaskManaged.reSearch();
				return false;
			});

			/*
			 * 清除
			 */
			$("#clearBtn").click(function() {
				$("#searchForm")[0].reset();
				return false;
			});
			
			/*
			 * 流程执行过程
			 * */
			$('#taskListTable tbody').on('click', 'button[name="viewExeProcess"]', function(){
				var taskId = $(this).attr("task_id");
				$("#exeProcessTable tbody").empty();
				$.post(ctx + "/process/task/listAction?now=" + new Date().getTime(),{taskId:taskId},function(data){
					var rtHtml = function(data){
						return "<tr>" +
								"<td>"+data.activityId+"</td>" +
								"<td>"+data.activityName+"</td>" +
								"<td>"+data.startTime+"</td>" +
								"<td>"+data.endTime+"</td>" +
								"</tr>";
					}
					var jsonData = JSON.parse(data);
					for(var i = 0 ; i < jsonData.data.length ; i++){
						$("#exeProcessTable tbody").append(rtHtml(jsonData.data[i]));
					}
					$("#exe-process-modal").modal("show");
				});
			});
			
			/*
			 * 历史批注
			 * */
			$('#taskListTable tbody').on('click', 'button[name="viewComment"]', function(){
				var taskId = $(this).attr("task_id");
				$("#commentTable tbody").empty();
				$.post(ctx + "/process/task//listHistoryComment?now=" + new Date().getTime(),{taskId:taskId},function(data){
					var rtHtml = function(data){
						return "<tr>" +
								"<td>"+data.time+"</td>" +
								"<td>"+data.userName+"</td>" +
								"<td>"+data.message+"</td>" +
								"</tr>";
					}
					var jsonData = JSON.parse(data);
					for(var i = 0 ; i < jsonData.data.length ; i++){
						$("#commentTable tbody").append(rtHtml(jsonData.data[i]));
					}
					$("#comment-modal").modal("show");
				});
			});
			
			/*
			 * 查看详情
			 * */
			$('#taskListTable tbody').on('click', 'button[name="viewDetail"]', function(){
				var id = $(this).attr("target_id"),
				taskId = $(this).attr("task_id"),
				processDefinitionId = $(this).attr("processDefinitionId");
				if(processDefinitionId.indexOf('leave') != -1){
					var form=$("form[name='leaveAuditForm']");
					$.ajax({
						"dataType" : 'json',
						"cache" : false,
						"type" : "POST",
						"data" : {},
						"url" : ctx + '/leave/get/'+id+"?now="+new Date().getTime(),
						"success" : function(data, textStatus, jqXHR){
							if (data.type == 'success') {
								$(form).binddata(data.data);
								$("div[name=leaveAuditDlg] h4.modal-title").html("查看详情");
								var lbTime = Number($(form).find('#leaveBeginTime').val()),
									leTime = Number($(form).find('#leaveEndTime').val());
								$(form).find('#leaveBeginTime').val(getStringDate(lbTime));
								$(form).find('#leaveEndTime').val(getStringDate(leTime));
								$(form).find('#taskId').val(taskId);
								$(form).find('#taskKey').val(taskKey);
								$(form).find('.form-group:last').remove();
								$(form).find('.modal-footer-dialog').remove();
								$("#leaveAuditDlg").modal("show");
							} else {
								BootstrapDialog.show({
									title : messages.public_cue_message,
									message : data.message,
									buttons : [ {
										label : messages.public_cue_sure,
										action : function(dialog) {
											dialog.close();
										}
									} ]
								});
							}
						}
					});
				}else if(processDefinitionId.indexOf('overtime') != -1){
					var form=$("form[name='overtimeAuditForm']");
					$.ajax({
						"dataType" : 'json',
						"cache" : false,
						"type" : "POST",
						"data" : {},
						"url" : ctx + '/overtime/get/'+id+"?now="+new Date().getTime(),
						"success" : function(data, textStatus, jqXHR){
							if (data.type == 'success') {
								$(form).binddata(data.data);
								$("div[name=overtimeAuditDlg] h4.modal-title").html("查看详情");
								var onTime = Number($(form).find('#onDutyTime').val()),
									ofTime = Number($(form).find('#offDutyTime').val()),
									isHolidays = Number($(form).find('#isHolidays').val());
								$(form).find('#onDutyTime').val(getStringDate(onTime));
								$(form).find('#offDutyTime').val(getStringDate(ofTime));
								$(form).find('#isHolidays').val(isHolidays ? "是" : "不是");
								$(form).find('#taskId').val(taskId);
								$(form).find('#taskKey').val(taskKey);
								$(form).find('.form-group:last').remove();
								$(form).find('.modal-footer-dialog').remove();
								$("#overtimeAuditDlg").modal("show");
							} else {
								BootstrapDialog.show({
									title : messages.public_cue_message,
									message : data.message,
									buttons : [ {
										label : messages.public_cue_sure,
										action : function(dialog) {
											dialog.close();
										}
									} ]
								});
							}
						}
					});
				}
			});
		}
	};

}();